package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.lang.utils.ObjectId;
import com.cloudlead.commonData.service.TbContainerInfoService;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.TbContainerInfo;
import com.cloudlead.commonData.vo.TbCustomerOrder;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.customerAction.service.ContainerInfoFindSer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/31.
 */
@Service
public class ContainerInfoFindSerImpl implements ContainerInfoFindSer {

    @Autowired
    TbContainerInfoService tbContainerInfoService;
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;
    @Autowired
    TbFleetOrderService tbFleetOrderService;


    @Override
    public Page<Object> containerInfoFind(String customerOrderId,int size,int page) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
            tr.add(new Term("flag","0"));//未删除
        //查询条件
        if(customerOrderId!=null&&(!"".equals(customerOrderId))) {
            tr.add(new Term("tb_customer_order_id",customerOrderId));//订单ID
        }

        Filter filter=new Filter( new Criteria( tr));
            pr.setPage(page);//页码
            pr.setSize(size);//每页数量
            pr.setFilter(filter);
            Sort sort = new Sort();//订单创建时间倒序
            sort.add(new Sort.Order(Sort.Direction.DESC, "saveDate"));
            pr.setSort(sort);
        Page<Object> po = new Page<Object>();
        Page<TbContainerInfo> px =tbContainerInfoService.findAll(pr);
        List<TbContainerInfo> lx=px.getContent();
        List<TbContainerInfo> lx1 = new ArrayList<TbContainerInfo>();
        for(Iterator<TbContainerInfo> it = px.iterator();it.hasNext();) {//遍历
            TbContainerInfo tci = it.next();
            if(tci.getTbCustomerOrder()!=null&&tci.getTbCustomerOrder().getId()!=null){//订单ID不为空
                TbCustomerOrder tco= tbCustomerOrderService.view(tci.getTbCustomerOrder().getId());//查出订单信息
                tci.setTbCustomerOrder(tco);
            }
            lx1.add(tci);
        }
        px.setContent(lx1);
        BeanUtils.copyProperties(px,po);//拷贝page
        return po;
    }

    @Override
    public Object containerInfoFindOne(String id) {
        TbContainerInfo tci = tbContainerInfoService.view(id);//查出箱信息
        if(tci.getTbCustomerOrder()!=null&&tci.getTbCustomerOrder().getId()!=null){//订单id不为空
            TbCustomerOrder tco= tbCustomerOrderService.view(tci.getTbCustomerOrder().getId());//查出订单信息
            tci.setTbCustomerOrder(tco);
        }
        if(tci.getFleetOrder()!=null&&tci.getFleetOrder().getId()!=null){//订单id不为空
            TbFleetOrder tfo= tbFleetOrderService.view(tci.getFleetOrder().getId());//查出订单信息
            tci.setFleetOrder(tfo);
        }
        return tci;
    }

    @Override
    public List<Object> containerInfoFindAll(String customerOrderId) {
        TbContainerInfo ci = new TbContainerInfo();
            TbCustomerOrder co = new TbCustomerOrder();
                co.setId(customerOrderId);
            ci.setTbCustomerOrder(co);
            ci.setFlag("0");
        List<TbContainerInfo> ltci = tbContainerInfoService.list(ci,null);
        List<Object> lo = new ArrayList<Object>();
        for(Iterator<TbContainerInfo> it = ltci.iterator();it.hasNext();) {//遍历
            TbContainerInfo tci = it.next();
            if(tci.getTbCustomerOrder()!=null&&tci.getTbCustomerOrder().getId()!=null){//订单ID不为空
                TbCustomerOrder tco= tbCustomerOrderService.view(tci.getTbCustomerOrder().getId());//查出订单信息
                tci.setTbCustomerOrder(tco);
            }
            lo.add(tci);
        }
        return lo;
    }
}
